package com.sly.fuxi.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author SLY
 * @date 2025/2/15
 */
@Configuration
public class RabbitMQConfig {

    /** 直连交换机 */
    public static final String DIRECT_EXCHANGE = "directExchange";
    /** 扇形交换机 */
    public static final String FANOUT_EXCHANGE = "fanoutExchange";
    /** 主题交换机 */
    public static final String TOPIC_EXCHANGE = "topicExchange";
    /** 延时交换机 */
    public static final String DELAYED_EXCHANGE = "delayedExchange";

    /** 直连交换机队列一 */
    public static final String DIRECT_QUEUE_1 = "directQueue1";
    /** 直连交换机队列二 */
    public static final String DIRECT_QUEUE_2 = "directQueue2";
    /** 直连交换机队列三 */
    public static final String DIRECT_QUEUE_3 = "directQueue3";
    /** 扇形交换机队列一 */
    public static final String FANOUT_QUEUE_1 = "fanoutQueue1";
    /** 扇形交换机队列二 */
    public static final String FANOUT_QUEUE_2 = "fanoutQueue2";
    /** 延时队列 */
    public static final String DELAYED_QUEUE = "delayQueue";
    /** 主题交换机队列一 */
    public static final String TOPIC_QUEUE_1 = "topicQueue1";
    /** 主题交换机队列二 */
    public static final String TOPIC_QUEUE_2 = "topicQueue2";
    /** 主题交换机队列三 */
    public static final String TOPIC_QUEUE_3 = "topicQueue3";

    // 直连交换机
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    public Queue directQueue1() {
        return new Queue(DIRECT_QUEUE_1);
    }

    @Bean
    public Queue directQueue2() {
        return new Queue(DIRECT_QUEUE_2);
    }

    @Bean
    public Queue directQueue3() {
        return new Queue(DIRECT_QUEUE_3);
    }

    @Bean
    public Binding directBinding1(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("direct_routing_key_1");
    }

    @Bean
    public Binding directBinding2(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with("direct_routing_key_2");
    }

    @Bean
    public Binding directBinding3(Queue directQueue3, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue3).to(directExchange).with("direct_routing_key_2");
    }

    // 扇形交换机
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    @Bean
    public Queue fanoutQueue1() {
        return new Queue(FANOUT_QUEUE_1);
    }

    @Bean
    public Queue fanoutQueue2() {
        return new Queue(FANOUT_QUEUE_2);
    }

    @Bean
    public Binding fanoutBinding1(Queue fanoutQueue1, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }

    @Bean
    public Binding fanoutBinding2(Queue fanoutQueue2, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }

    // 主题交换机
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    @Bean
    public Queue topicQueue1() {
        return new Queue(TOPIC_QUEUE_1);
    }

    @Bean
    public Queue topicQueue2() {
        return new Queue(TOPIC_QUEUE_2);
    }

    @Bean
    public Queue topicQueue3() {
        return new Queue(TOPIC_QUEUE_3);
    }

    @Bean
    public Binding topicBinding1(Queue topicQueue1, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue1).to(topicExchange).with("topic_routing_key.*");
    }

    @Bean
    public Binding topicBinding2(Queue topicQueue2, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with("topic_routing_key.one");
    }

    @Bean
    public Binding topicBinding3(Queue topicQueue3, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue3).to(topicExchange).with("topic_routing_key.#");
    }

    // 延时交换机
    @Bean
    public CustomExchange delayedExchange() {
        // 定义延时交换器
        Map<String, Object> args = new HashMap<>();
        // 指定交换器类型为 x-delayed-message
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue delayedQueue() {
        // 定义延时队列
        return new Queue(DELAYED_QUEUE, true);
    }

    @Bean
    public Binding binding(Queue delayedQueue, CustomExchange delayedExchange) {
        // 将队列绑定到延时交换器
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with("delay_routing_key").noargs();
    }

}
